//
//  ChangeMyNewsController.m
//  PosApp
//
//  Created by 李英新 on 2018/10/3.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "ChangeMyNewsController.h"
#import "ForgetPWController.h"
#import "ChangePayPWController.h"


@interface ChangeMyNewsController ()

@property (weak, nonatomic) IBOutlet UIImageView *UserImage;
@property (weak, nonatomic) IBOutlet UITextField *NameText;
@property (weak, nonatomic) IBOutlet UITextField *iPhoneText;




@end

@implementation ChangeMyNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.self
    self.navigationItem.title = @"修改资料";
    self.UserImage.backgroundColor = [UIColor grayColor];
    self.UserImage.layer.cornerRadius = self.UserImage.bounds.size.height/2;
    self.UserImage.layer.masksToBounds = YES;
    [self.UserImage sd_setImageWithURL:[NSURL URLWithString:KUserImage]];
    self.NameText.text = [NSString stringWithFormat:@"%@",KUserName];
    self.iPhoneText.text = [NSString stringWithFormat:@"%@",KUseriPhone];
    
}

- (IBAction)ClickOnBtnToChooseImage:(id)sender {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;//隐藏视频
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (photos.count > 0) {
            self.UserImage.image = photos[0];
        }
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

- (IBAction)ClickOnBtnToChangeLoginPW:(id)sender {
    ForgetPWController * PWC = [[ForgetPWController alloc] init];
    PWC.title = @"重置登录密码";
    [self.navigationController pushViewController:PWC animated:YES];
}
- (IBAction)ClickOnBtntoChangeAliPayPW:(id)sender {
    ChangePayPWController * PayC = [[ChangePayPWController alloc] init];
    [self.navigationController pushViewController:PayC animated:YES];
}
- (IBAction)ClickOnBtnToBaoCun:(id)sender {
    if (self.UserImage.image == nil) {
        [MBProgressHUD showAlertViewWithText:@"请选择上传的头像"];
        return;
    }
    
    if (self.NameText.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"姓名不能为空"];
        return;
    }

    if (![MainObjectWay IsMobileWithString:self.iPhoneText.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    NSMutableArray * ImageData = [[NSMutableArray alloc] init];
    [ImageData addObject:self.UserImage.image];
    
    NSMutableDictionary * Dic = [[NSMutableDictionary alloc] init];
    
    [MainObjectWay PostWithMethod:@"upload" andAction:@"Server" andAddParam:NO parameters:Dic Name:@"file" formData:ImageData success:^(id responseObject) {
        NSLog(@"上传成功信息 = %@",responseObject);
        NSLog(@"消息 = %@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSString * ImageStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"link"]];
            NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
            [dic setObject:KUseriPhone forKey:@"phone"];
            [dic setObject:self.NameText.text forKey:@"nickname"];
            [dic setObject:self.iPhoneText.text forKey:@"tel"];
            [dic setObject:ImageStr forKey:@"avatar"];
            [dic setObject:@"1" forKey:@"sex"];
            
            [MainObjectWay PostWithMethod:@"editInfo" andAction:@"User" andAddParam:YES parameters:dic success:^(id responseObject) {
                NSLog(@"提交结果信息 = %@",responseObject);
                if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
                }
            } failure:^(NSError *error) {
                NSLog(@"错误信息 = %@",error);
                [MBProgressHUD showAlertViewWithText:@"网络请求错误"];
            }];
        }else{
            [MBProgressHUD showAlertViewWithText:@"提交失败"];
        }
    } failure:^(NSError *error) {
        NSLog(@"上传失败信息 = %@",error);
        [MBProgressHUD showAlertViewWithText:@"网络请求错误"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
